﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.CustomerManage;
using System.IO;

namespace MINERP.CustomerManage.StartOrder
{
    public partial class FrmStartOrder2 : Form
    {
        //参数
        private BLL.CustomerManage.StartOrderBLL SOBLL = new MINERP.BLL.CustomerManage.StartOrderBLL();
        List<StartOrderBLL.ProductInfo> productList;                                      //原始的产品默认信息(从数据库读取的原始信息)
        List<StartOrderBLL.STProductDisplay> ListProductDisplay;                        //原始产品的显示默认信息(根据数据库作了修改)
        List<StartOrderBLL.STProductDisplay> FilterDisplayProduct = new List<StartOrderBLL.STProductDisplay>();//按类别分类的原始产品信息 
        List<StartOrderBLL.ProductInfo> ListProduct = new List<StartOrderBLL.ProductInfo>();    //订单的产品集合(存储使用)

        int OrderID = 0;
        string BagCode = "";

        //属性
        /// <summary>
        /// 开单人
        /// </summary>
        int UserID { get; set; }

        public FrmStartOrder2()
        {
            InitializeComponent();
        }

        #region Load方法
        private void FrmStartOrder2_Load(object sender, EventArgs e)
        {
            UserID = 1;

            //产品集合
            productList = SOBLL.GetProductList();
            //订单包号
            SOBLL.AddOrderBagTemp(out OrderID, out BagCode, UserID);
            if (btnCodeType.Text == "手动")
                SetOrderBagCode();
            //产品集合
            productList = SOBLL.GetProductList();
            //产品的显示信息集合
            ListProductDisplay = new List<StartOrderBLL.STProductDisplay>();
            foreach (StartOrderBLL.ProductInfo defaltProduct in productList)
            {
                StartOrderBLL.STProductDisplay display = new StartOrderBLL.STProductDisplay();
                display.ProductID = defaltProduct.ProductID;
                display.ProductName = defaltProduct.ProductName;
                display.ProductCategoryID = defaltProduct.ProductCategoryID;
                display.ProductCategoryName = defaltProduct.ProductCategoryName;
                float rh = defaltProduct.Height;
                float rw = defaltProduct.Width;
                display.ProductHW = string.Format("{0:0.##}", rh) + "×" + string.Format("{0:0.##}", rw);
                ListProductDisplay.Add(display);
            }
            //加载产品模板列表
            AddProductModel();
            //加载代开单列表
            AddStartOrderPath();
        }
        #endregion

        #region 添加产品列表
        /// <summary>
        /// 添加产品列表
        /// </summary>
        private void AddProductModel()
        {
            bcProduct.Groups.Clear();
            foreach (StartOrderBLL.STProductDisplay product in ListProductDisplay)
            {
                int productID = product.ProductID;
                int productCategoryID = product.ProductCategoryID;
                bool IsExistGroup = false;
                foreach (DevExpress.XtraNavBar.NavBarGroup g in bcProduct.Groups)
                {
                    if (Convert.ToInt16(g.Tag) == productCategoryID)
                    {
                        IsExistGroup = true;    //表示组已存在
                        break;
                    }
                }
                if (IsExistGroup == false)  //如果组不存在，就添加
                {
                    DevExpress.XtraNavBar.NavBarGroup group = new DevExpress.XtraNavBar.NavBarGroup();
                    group.Caption = product.ProductCategoryName;
                    group.Tag = productCategoryID;
                    group.Expanded = false;
                    bcProduct.Groups.Add(group);
                }
            }
        }
        #endregion 添加产品列表

        #region 向产品列表组内添加产品
        /// <summary>
        /// 向产品列表组内添加产品
        /// </summary>
        private void bcProduct_ActiveGroupChanged(object sender, DevExpress.XtraNavBar.NavBarGroupEventArgs e)
        {
            e.Group.GroupStyle = DevExpress.XtraNavBar.NavBarGroupStyle.ControlContainer;
            DevExpress.XtraGrid.GridControl grid = gcProductCategory;
            grid.Visible = true;
            FilterDisplayProduct.Clear();       //清除上一类别的信息
            int productCategoryID = Convert.ToInt16(e.Group.Tag);
            foreach (StartOrderBLL.STProductDisplay product in ListProductDisplay)
            {
                if (product.ProductCategoryID == productCategoryID)
                {
                    FilterDisplayProduct.Add(product);
                }
            }
            grid.DataSource = FilterDisplayProduct;
            grid.RefreshDataSource();
            grid.Dock = DockStyle.Fill;
            e.Group.ControlContainer.Controls.Add(grid);
        }
        #endregion

        #region 待开单目录结构
        /// <summary>
        /// 待开单订单的目录
        /// </summary>
        private void AddStartOrderPath()
        {
            string totalPath = @"D:\HW\开单测试目录";
            string[] directorys = Directory.GetDirectories(totalPath);
            foreach (string directory in directorys)
            {
                if (Directory.GetDirectories(directory).Length > 1)
                {
                    DirectoryInfo directoryInfo = new DirectoryInfo(directory);
                    TreeNode node = new TreeNode(directoryInfo.Name);
                    node.Tag = directoryInfo.FullName;
                    //node.ImageIndex = 1;
                    tvOrderPath.Nodes.Add(node);
                    string[] subDirectorys = Directory.GetDirectories(directory);
                    foreach (string subDirectory in subDirectorys)
                    {
                        DirectoryInfo subInfo = new DirectoryInfo(subDirectory);
                        if (subInfo.Name != "已开单")
                        {
                            TreeNode subNode = new TreeNode(subInfo.Name);
                            subNode.Tag = subInfo.FullName;
                            //subNode.ImageIndex = 0;
                            node.Nodes.Add(subNode);
                            string[] ssubDirectorys = Directory.GetDirectories(subDirectory);
                            foreach (string ssubDirectory in ssubDirectorys)
                            {
                                DirectoryInfo ssubInfo = new DirectoryInfo(ssubDirectory);
                                TreeNode ssubNode = new TreeNode(ssubInfo.Name);
                                ssubNode.Tag = ssubInfo.FullName;
                                subNode.Nodes.Add(ssubNode);
                                //ssubNode.ImageIndex = 0;
                            }
                        }
                    }
                }
            }
        }
        #endregion

        #region 设置订单包号
        /// <summary>
        /// 设置订单包号
        /// </summary>
        private void SetOrderBagCode()
        {
            btnCodeType.Text = btnCodeType.Text == "自动" ? "手动" : "自动";
            if (btnCodeType.Text == "自动")
            {
                txtOrderBagCode.Enabled = false;
                txtOrderBagCode.Text = BagCode;
            }
            else
            {
                txtOrderBagCode.Enabled = true;
                txtOrderBagCode.Text = "";
            }
        }
        #endregion

        #region "自动"，"手动"操作
        //"自动"，"手动"操作
        private void btnCodeType_Click(object sender, EventArgs e)
        {
            SetOrderBagCode();
        }
        #endregion

        #region 待开单目录的拖曳操作
        //待开单目录的拖曳操作
        private void tvOrderPath_ItemDrag(object sender, ItemDragEventArgs e)
        {
            TreeNode tree = (TreeNode)e.Item;
            if (tree.Parent == null)                    //商户节点，不执行拖曳操作
                return;
            else
            {
                List<string> productPaths = new List<string>();
                if (tree.Nodes.Count > 0)               //客户节点
                {
                    //加载客户下所有的产品,加载后变为粉红色
                    foreach (TreeNode n in tree.Nodes)
                    {
                        productPaths.Add(n.Tag.ToString());
                    }
                }
                else                                    //产品节点
                {
                    //加载产品,加载后变为粉红色
                    productPaths.Add(tree.Tag.ToString());
                }
                DataObject data = new DataObject();
                data.SetData("list", productPaths);
                DoDragDrop(data, DragDropEffects.All);
            }
        }
        #endregion

        /// <summary>
        /// 加载客户的所有产品(产品实物)
        /// </summary>
        private void AddCtmProducts(List<string> productPaths)
        {
            if (gvProductList.RowCount == 0)                    //还未添加产品
            {
                foreach (string productPath in productPaths)
                {
                    AddProductNew(productPath);                 //加载产品实物(还未添加模板)
                }
            }
            else
            {
                int focusedRow = gvProductList.FocusedRowHandle;
                StartOrderBLL.ProductInfo product = (StartOrderBLL.ProductInfo)gvProductList.GetRow(focusedRow);
                if (product.ProductType == 2)                   //当前产品只加载了模板，
                {
                    if (productPaths.Count > 1)
                    {
                        MessageBox.Show("不能为一个模板添加多个产品!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                    {
                        AddProduct(product);
                    }
                }
            }
        }

        /// <summary>
        /// 加载产品实物(已有模板)
        /// </summary>
        private void AddProduct(StartOrderBLL.ProductInfo product)
        { 
            
        }
        /// <summary>
        /// 加载产品实物(还未添加模板)
        /// </summary>
        private void AddProductNew(string productPath)
        {
            int index1 = productPath.LastIndexOf("\\");
            string tempName=productPath.Substring(index1+1);
            string productPath1 = productPath.Substring(0, index1);
            int index2 = productPath1.LastIndexOf("\\");
            string ctmName = productPath1.Substring(index2 + 1);
            DirectoryInfo info = new DirectoryInfo(productPath);
            StartOrderBLL.ProductInfo newProduct = new StartOrderBLL.ProductInfo();
            newProduct.CtmName = ctmName;
            newProduct.Height = 0;
            newProduct.OrilOrderPath = productPath1 + "\\已开单\\" + tempName;
            newProduct.PageNum = 0;
            newProduct.PaperFaceID = 0;
            newProduct.PaperTypeID = 0;
            newProduct.PhotoAllArea = 0;
            newProduct.PhotoNum = 0;
            newProduct.ProductBarCode = "2222222222";
            newProduct.ProductID = 0;
            newProduct.ProductName = "";
            newProduct.ProductNum = 0;
            newProduct.ProductRemark = "";
            newProduct.ProductSerialID = currentSerialID;
            newProduct.ProductType = 1;
            newProduct.techs = null;
            newProduct.Unit = "";
            newProduct.Width = 0;
        }

        //-----------------------------------------------
        //----------产品明细的操作-----------------------
        //-----------------------------------------------
        int currentSerialID = 0;                                //当前产品的系列ID,用于标识产品

        //-----------------------------------------------
        //----------产品明细操作结束---------------------
        //-----------------------------------------------
    }
}
